<?php

namespace App\Http\Controllers;

use App\Models\Project;
use App\Models\ProjectHasExtra;
use App\Models\ReservationLog;
use Carbon\Carbon;
use Illuminate\Http\Request;

class ProjectController extends Controller
{

    public function getDetail(Request $request)
    {
        $id = $request->get("id");
        if (!$id)
            return response()
                ->json([
                    'code' => HTTP_BAD_REQUEST,
                    'msg' => '错误的项目',
                ]);
        $id = explode(",", $id);
        $data = Project::query()
            ->whereIn('id', $id)
            ->get();

        return response()
            ->json([
                'code' => HTTP_OK,
                'msg' => "OK",
                'data' => $data,
            ]);
    }

    public function getReservationDetail(Request $request)
    {
        $id = $request->get("id");
        if (!$id)
            return response()
                ->json([
                    'code' => HTTP_BAD_REQUEST,
                    'msg' => '错误的项目',
                ]);
        $id = explode(",", $id);
        $date = $request->get('date') ?: now();
        $date = Carbon::parse($date);

        $projectData = Project::query()
            ->whereIn('id', $id)
            ->where('is_show', 1)
            ->where('time_interval', '>', 0)
            ->orderBy('time_interval', 'desc')
            ->first();

        $extras = ProjectHasExtra::query()
            ->whereIn('project_id', $id)
            ->where('enable', 1)
            ->where(function ($query) use ($date) {
                // 如果 status = 0，直接返回记录
                $query->where('day_type', 0)
                    // 如果 status = 1，检查 start_date 和 end_date 是否包含指定日期
                    ->orWhere(function ($query) use ($date) {
                        $query->where('day_type', 1)
                            ->whereDate('start_date', '<=', $date)
                            ->whereDate('end_date', '>=', $date);
                    });
            })
            ->get();
        $reservations = ReservationLog::query()
            ->whereHas('projects', function ($query) use ($id) {
                $query->whereIn("id", $id);
            })
            ->where('status', 0)
            ->whereDate('date', $date)
            ->get();
        $timeResult = [];
        foreach ($projectData->time_group as $time) {
            $extra = Project::checkExtraIsMatch($extras, $time);
            if ($extra) {
                if ($extra->extra_type) {
                    $timeResult[$time] = $extra->up;
                } else
                    $timeResult[$time] = 0;
            } else
                $timeResult[$time] = $projectData->limit_count;
            if ($timeResult[$time] < 1) continue;

            foreach ($reservations as $reservation) {
                if (hasOverlap($time, $reservation->time)) {
                    $timeResult[$time] -= 1;
                }
            }
        }

        return response()
            ->json([
                'code' => HTTP_OK,
                'msg' => "OK",
                'data' => $timeResult,
            ]);
    }

    public
    function getProjectTimeRange()
    {

    }
}
